// Entering shamal as passenger v2.12 by BeckzyBoi.

#include <a_samp>

new InShamal[MAX_PLAYERS];
new Float:ShamalPos[MAX_VEHICLES][3];
new sExplode[MAX_VEHICLES];
new tCount[MAX_VEHICLES];

new Float:difc[][] =
{
        {1.13, 0.05, 1.10, 0.0},
        {1.13, 2.35, 1.10, 180.0},
        {1.13, 4.65, 1.10, 180.0},
        {1.13, 1.05, 1.10, 0.0},
        {1.13, 3.45, 1.10, 180.0},
        {1.13, 5.85, 1.10, 180.0},
        {1.13, 0.39, 0.56, 0.0},
        {1.13, 2.69, 0.56, 180.0},
        {1.13, 4.99, 0.56, 180.0},
        {1.13, 0.71, 0.56, 0.0},
        {1.13, 3.79, 0.56, 180.0},
        {1.13, 6.19, 0.56, 180.0},
        {0.00, 0.30, 1.10, 0.0}
};

#define objects_per_shamal 14

#define SETY_DE 5.87
#define SETZ_DE 0.75

public OnFilterScriptInit()
{
        print("---------------------------------------");
        print("Loaded 'Entering shamal as passenger'");
        print("v2.12 by BeckzyBoi");
        print("---------------------------------------");
        return 1;
}

forward ExplodeShamal(vehicleid);

stock CreateShamalInt(vehicleid, Float:X, Float:Y, Float:Z)
{
        CreateObject(14404, X, Y, Z, 0.0, 0.0, 0.0);
        CreateObject(1562, X+difc[0][0], Y+difc[0][1], Z-difc[0][2], 0.0, 0.0, difc[0][3]);
        CreateObject(1562, X+difc[1][0], Y-difc[1][1], Z-difc[1][2], 0.0, 0.0, difc[1][3]);
        CreateObject(1562, X+difc[2][0], Y-difc[2][1], Z-difc[2][2], 0.0, 0.0, difc[2][3]);
        CreateObject(1562, X-difc[3][0], Y-difc[3][1], Z-difc[3][2], 0.0, 0.0, difc[3][3]);
        CreateObject(1562, X-difc[4][0], Y-difc[4][1], Z-difc[4][2], 0.0, 0.0, difc[4][3]);
        CreateObject(1562, X-difc[5][0], Y-difc[5][1], Z-difc[5][2], 0.0, 0.0, difc[5][3]);
        CreateObject(1563, X+difc[6][0], Y+difc[6][1], Z-difc[6][2], 0.0, 0.0, difc[6][3]);
        CreateObject(1563, X+difc[7][0], Y-difc[7][1], Z-difc[7][2], 0.0, 0.0, difc[7][3]);
        CreateObject(1563, X+difc[8][0], Y-difc[8][1], Z-difc[8][2], 0.0, 0.0, difc[8][3]);
        CreateObject(1563, X-difc[9][0], Y-difc[9][1], Z-difc[9][2], 0.0, 0.0, difc[9][3]);
        CreateObject(1563, X-difc[10][0], Y-difc[10][1], Z-difc[10][2], 0.0, 0.0, difc[10][3]);
        CreateObject(1563, X-difc[11][0], Y-difc[11][1], Z-difc[11][2], 0.0, 0.0, difc[11][3]);
        CreateObject(14405, X, Y-difc[12][1], Z-difc[12][2], 0.0, 0.0, difc[12][3]);
        ShamalPos[vehicleid][0] = X, ShamalPos[vehicleid][1] = Y, ShamalPos[vehicleid][2] = Z;
}

stock SetPlayerPosInShamal(playerid, shamalid)
{
        SetPlayerPos(playerid, ShamalPos[shamalid][0], ShamalPos[shamalid][1]-SETY_DE, ShamalPos[shamalid][2]-SETZ_DE);
        SetPlayerFacingAngle(playerid, 0.0);
        SetCameraBehindPlayer(playerid);
        InShamal[playerid] = shamalid;
}

stock ShamalExists(vehicleid)
{
        if (floatsqroot(ShamalPos[vehicleid][0] + ShamalPos[vehicleid][1] + ShamalPos[vehicleid][2]))
        {
                return 1;
        }
        return 0;
}

Float:randomEx(randval)
{
        new rand1 = random(2), rand2;
        return float(rand1 == 0 ? rand2 - random(randval) : rand2 + random(randval));
}

public OnPlayerConnect(playerid)
{
        InShamal[playerid] = 0;
        return 1;
}

stock get_available_objects()
{
        new objects = 0;
        for (new i = 1; i <= MAX_OBJECTS; i++) {
                if (IsValidObject(i)) objects ++;
        }
        return MAX_OBJECTS-objects;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
        if (ispassenger != 0)
        {
                if (GetVehicleModel(vehicleid) == 519)
                {
                        if (ShamalExists(vehicleid) == 0)
                        {
                                if (get_available_objects() > (MAX_OBJECTS-objects_per_shamal)) return 1;
                                CreateShamalInt(vehicleid, randomEx(3000), randomEx(3000), float(random(100)+800));
                        }
                        SetPlayerPosInShamal(playerid, vehicleid);
                }
        }
        return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
        if (newkeys == 16 && InShamal[playerid] != 0)
        {
                new Float:X, Float:Y, Float:Z, Float:A;
                GetVehiclePos(InShamal[playerid], X, Y, Z);
                GetVehicleZAngle(InShamal[playerid], A);
                X += (5.0*floatsin(-(A-45.0), degrees)), Y += (5.0*floatcos(-(A-45.0), degrees));
                SetPlayerInterior(playerid, 0);
                SetPlayerPos(playerid, X, Y, Z-0.94);
                SetPlayerFacingAngle(playerid, A);
                InShamal[playerid] = 0;
        }
        return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
        InShamal[playerid] = 0;
        return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
        if (GetVehicleModel(vehicleid) == 519 && ShamalExists(vehicleid) != 0)
        {
                CreateExplosion(ShamalPos[vehicleid][0], ShamalPos[vehicleid][1], ShamalPos[vehicleid][2], 2, 15.0);
                sExplode[vehicleid] = SetTimerEx("ExplodeShamal", 700, 0, "d", vehicleid);
                tCount[vehicleid] = true;
        }
        return 1;
}

public OnVehicleSpawn(vehicleid)
{
        tCount[vehicleid] = false;
        for (new i = 0; i != MAX_PLAYERS; i++)
        {
                if (InShamal[i] == vehicleid) SetPlayerHealth(i, 0.0);
        }
        return 1;
}

public ExplodeShamal(vehicleid)
{
        KillTimer(sExplode[vehicleid]);
        if (tCount[vehicleid])
        {
                CreateExplosion(ShamalPos[vehicleid][0], ShamalPos[vehicleid][1], ShamalPos[vehicleid][2], 2, 15.0);
                sExplode[vehicleid] = SetTimerEx("ExplodeShamal", random(1300) + 100, 0, "d", vehicleid);
        }
}
